package com.javaweb.service.impl;

import com.javaweb.aop.GranularType;
import com.javaweb.aop.TimeMeasure;
import com.javaweb.dao.UserDao;
import com.javaweb.model.User;
import com.javaweb.service.UserService;
import com.javaweb.service.dto.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: zxzx
 * @date: 2024/9/23 12:36
 */
@TimeMeasure(granular = GranularType.MIL_SECOND)
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao ;
    @Override
    public User login(String username, String password) {
        User user = new User() ;
        if( user.getName().equals( "123")) {

        }
        return userDao.selectOne(username, password) ;
    }

    @Override
    public List<User> findUserByName(String username) {
        User user = new User() ;
        if( user.getName().equals( "123")) {

        }
        return userDao.selectByName(username) ;
    }
    @Override
    public User queryByName(String username) {
        return userDao.findByName(username);
    }

    /**
     * 注册用户
     * @param user 用户对象
     * @return 0 表示注册失败，1 表示注册成功
     */
    @Override
    public int register(User user) {
        int result = userDao.insertUser(user);
        return result > 0 ? 1 : 0;
    }

    /**
     * 分页查询用户列表
     * @param userCondition 用户查询条件
     * @param pageNum 当前页码
     * @param pageSize 每页显示数量
     * @return 分页结果
     */
    @Override
    public PageResult queryUsers(User userCondition, Integer pageNum, Integer pageSize) {
        List<User> users = userDao.selectUsersByCondition(userCondition, pageNum, pageSize);
        int total = userDao.countUsersByCondition(userCondition);
        return new PageResult(users, total, pageNum, pageSize);
    }

    /**
     * 分配角色给用户
     * @param userId 用户ID
     * @param roleIds 角色ID列表
     */
    @Override
    public void addAssignRole(String userId, List<Integer> roleIds) {
        userDao.assignRolesToUser(userId, roleIds);
    }
}
